import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.Scanner;



public class zhaoxiao {

    private static final Scanner scanner = new Scanner(System.in);


    public static void main(String[] args) {

        // 创建学生组

        Group group = new Group();


        // 初始化学生数据

        group.addStudent(new Student("张恒益", "1", "2023015444", 85));

        group.addStudent(new Student("李佳乐", "1", "2023015666", 90));

        group.addStudent(new Student("张建成", "1", "2023015777", 78));

        group.addStudent(new Student("杜佳硕", "1", "2013015555", 78));


        while (true) {

            System.out.println("\n请选择操作：");

            System.out.println("1. 随机抽取学生并加分");

            System.out.println("2. 显示所有学生信息");

            System.out.println("3. 显示学生加分历史");

            System.out.println("4. 退出程序");


            int choice = getIntInput("请输入选项（1-4）：");


            switch (choice) {

                case 1:

                    randomSelectAndAddScore(group);

                    break;

                case 2:

                    group.displayAllStudents();

                    break;

                case 3:

                    displayScoreHistory(group);

                    break;

                case 4:

                    System.out.println("程序已退出");

                    return;

                default:

                    System.out.println("无效选项，请重新选择");

            }

        }

    }


    private static void randomSelectAndAddScore(Group group) {

        try {

            Student selected = group.getRandomStudent();

            System.out.println("\n随机抽取的学生：" + selected);


            int points = getIntInput("请输入要加的分数：");

            if (points <= 0) {

                System.out.println("分数必须为正数！");

                return;

            }


            System.out.print("请输入加分原因：");

            String reason = scanner.nextLine();


            selected.addScore(points, reason);


        } catch (Exception e) {

            System.out.println("操作失败：" + e.getMessage());

        }

    }


    private static void displayScoreHistory(Group group) {

        System.out.print("请输入要查看的学生姓名：");

        String name = scanner.nextLine();


        for (Student student : group.getStudents()) {

            if (student.getName().equals(name)) {

                List<ScoreRecord> history = student.getScoreHistory();

                if (history.isEmpty()) {

                    System.out.println("该学生暂无加分记录");

                } else {

                    System.out.println("\n" + student.getName() + "的加分历史：");

                    for (ScoreRecord record : history) {

                        System.out.println(record);

                    }

                }

                return;

            }

        }

        System.out.println("未找到该学生");

    }


    private static int getIntInput(String prompt) {

        while (true) {

            try {

                System.out.print(prompt);

                return Integer.parseInt(scanner.nextLine());

            } catch (NumberFormatException e) {

                System.out.println("请输入有效的数字！");

            }

        }

    }
}

